<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改售后厂家')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
    <link href="../static/css/bootstrap-select.min.css" th:href="@{/css/bootstrap-select.min.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-afterFactory-edit" th:object="${zsyhAfterFactory}">
            <input name="id" th:field="*{id}" type="hidden">
            <input id="factoryProvinceName" name="factoryProvinceName" class="form-control" type="hidden">
            <input id="factoryCityName" name="factoryCityName"  class="form-control" type="hidden">
            <input id="factoryDistrictName" name="factoryDistrictName" class="form-control" type="hidden">
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家名称：</label>
                        <div class="col-sm-8">
                            <input name="factoryName" th:field="*{factoryName}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家负责人：</label>
                        <div class="col-sm-8">
                            <input name="factoryPerson" th:field="*{factoryPerson}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">负责人手机号：</label>
                        <div class="col-sm-8">
                            <input name="factoryPhone" th:field="*{factoryPhone}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">邮件：</label>
                        <div class="col-sm-8">
                            <input name="factoryEmail" th:field="*{factoryEmail}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">营业执照：</label>
                        <div class="col-sm-8">
                            <input type="hidden" name="factoryLicence" th:field="*{factoryLicence}">
                            <div class="file-loading">
                                <input class="form-control file-upload" id="factoryLicence" name="file" type="file">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">营业执照代码：</label>
                        <div class="col-sm-8">
                            <input name="factoryLicenceCode" th:field="*{factoryLicenceCode}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">所属区域：</label>
                        <div class="col-sm-8">
                            <select class="selectpicker form-control" onchange="initCity()" id="factoryProvice" name="factoryProvice" required>
                                <option th:value="*{factoryProvice}" th:text="*{factoryProvinceName}">&#45;&#45;省份&#45;&#45;</option>
                            </select>
                            <select class="selectpicker form-control" onchange="initRegion()" id="factoryCity" name="factoryCity" required>
                                <option th:value="*{factoryCity}" th:text="*{factoryCityName}">&#45;&#45;城市&#45;&#45;</option>
                            </select>
                            <select class="selectpicker form-control" id="factoryDistrict" name="factoryDistrict" required>
                                <option th:value="*{factoryDistrict}" th:text="*{factoryDistrictName}">&#45;&#45;县区&#45;&#45;</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家详细地址：</label>
                        <div class="col-sm-8">
                            <input name="factoryAddress" th:field="*{factoryAddress}" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否使用自购件：</label>
                        <div class="col-sm-8">
                            <select name="isSelfParts" class="form-control m-b" th:with="type=${@dict.getType('zsyh_yesno')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{isSelfParts}"></option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">备注：</label>
                        <div class="col-sm-8">
                            <textarea name="factoryRemarks" class="form-control">[[*{factoryRemarks}]]</textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script src="../static/js/bootstrap-select.min.js" th:src="@{/js/bootstrap-select.min.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "zsyh/afterFactory";
        $("#form-afterFactory-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $("#factoryProvinceName").val($('#factoryProvice option:selected').text());
                $("#factoryCityName").val($('#factoryCity option:selected').text());
                $("#factoryDistrictName").val($('#factoryDistrict option:selected').text());
                $.operate.save(prefix + "/edit", $('#form-afterFactory-edit').serialize());
            }
        }

        $(document).ready(function(){
            // 地区下拉框初始化
            $("#factoryProvice").selectpicker({width:110, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#factoryCity").selectpicker({width:110, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#factoryDistrict").selectpicker({width:110, liveSearch: true, liveSearchPlaceholder: "搜索" });
            //初始化数据，去掉当前已经选过的数据
            // 动态加载省份选项数据
            initSelectOptions1("factoryProvice","0",$("#factoryProvice").text().trim());
            // 动态加载城市选项数据
            initSelectOptions1("factoryCity",$("#factoryProvice").val(),$("#factoryCity").text().trim());
            // 动态加载县区选项数据
            initSelectOptions1("factoryDistrict",$("#factoryCity").val(),$("#factoryDistrict").text().trim());
        });// onReady

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions(selectId,parentId) {
            debugger;
            var selectObj = $("#" + selectId);
            $.ajax({
                url : ctx + "zsyh/factory/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {
                    debugger;
                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        selectObj.append(new Option(optionText, optionValue));
                    }

                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions1(selectId,parentId,text) {
            debugger;
            var selectObj = $("#" + selectId);
            $.ajax({
                url : ctx + "zsyh/factory/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {
                    debugger;
                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        if(optionText != text){
                            selectObj.append(new Option(optionText, optionValue));
                        }
                    }

                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 根据选择的省份动态初始化城市options
         * @returns
         */
        function initCity() {
            //清除城市和县区数据
            $("#factoryCity option").text("--请选择--");
            $("#factoryDistrict option").text("--请选择--");
            // 当省份变动时，初始化城市和清空县区和街道
            var provinceSel = $("#factoryProvice").val();
            initSelectOptions("factoryCity", provinceSel);
            $("#factoryDistrict").find("option:not(:first)").remove();
            $("#factoryDistrict").selectpicker('refresh');
        }

        /**
         * 根据选择的城市动态初始化区域options
         * @returns
         */
        function initRegion() {
            // 选择城市
            var citySel = $("#factoryCity").val();
            initSelectOptions("factoryDistrict", citySel);
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/uploadOSS',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            }).on("filebatchselected", function(event, files) {
                $(this).fileinput("upload");
            })
            $(this).fileinput('_initFileActions');
        });
    </script>
</body>
</html>